package com.wlyuan.open.controller;

import com.wlyuan.open.application.sms.service.SmsService;
import com.wlyuan.open.constants.SmsType;
import com.wlyuan.open.controller.resolver.OpenSmsResolver;
import com.wlyuan.open.dto.OpenSmsCodeDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author lijing
 */
@Slf4j
@Api(tags = "短信验证码")
@RequestMapping("/sms")
@Validated
@RestController
@RequiredArgsConstructor
public class OpenSmsController {
    private final SmsService smsService;
    private final OpenSmsResolver smsResolver;

    @ApiOperation("发送短信验证码")
    @PostMapping("/code")
    public void send(@Valid @RequestBody OpenSmsCodeDTO sms) {
        smsResolver.resolver(sms);
        smsService.send(sms);
    }

    @ApiOperation("发送注册短信验证码")
    @PostMapping("/register/code")
    public void register(@Valid @RequestBody OpenSmsCodeDTO sms) {
        sms.setType(SmsType.REGISTER.getValue());
        smsResolver.resolver(sms);
        smsService.send(sms);
    }

    @ApiOperation("发送绑定管理员账号短信验证码")
    @PostMapping("/bind/code")
    public String bind(@Valid @RequestBody OpenSmsCodeDTO sms) {
        sms.setType(SmsType.BIND.getValue());
        var mobile = smsResolver.resolverBind(sms);
        smsService.send(sms);
        return mobile;
    }
}
